//
//  MSCollectionManager.m
//  MSJD
//
//  Created by xiangbo on 2018/12/17.
//  Copyright © 2018 xiangbo. All rights reserved.
//

#import "MSCollectionManager.h"

@implementation MSCollectionManager

static NSMutableArray *collectArray;

+ (void)initialize{
    collectArray = [NSMutableArray array];
}

+ (void)collectAction:(NSString *)tid isCollect:(BOOL)isCollect completion:(void (^)(BOOL))completion{
    if (tid.length == 0) {
        if (completion) {
            completion(NO);
        }
        return;
    }
    if ([UserAccesser userId].length > 0) {
        NSString *path = isCollect ? @"v1/favor/add" : @"v1/favor/remove";
        [MSRequestClient POST:path parameters:@{@"uid": [UserAccesser userId],@"tid": tid ?: @""} responseClassType:MappedModel.class responseCache:nil success:^(id responseObject) {
            BOOL success = ((MappedModel *)responseObject).code.integerValue == 200 ? YES : NO;
            if (completion) {
                completion(success);
            }
            
        } failure:^(NSError *error) {
            if (completion) {
                completion(NO);
            }
        }];
    }else{
        if (isCollect) {
            [collectArray addObject:tid];
        }else{
            [collectArray removeObject:tid];
        }
       
        if (completion) {
            completion(YES);
        }
    }
}

+ (void)checkIsCollected:(NSString *)tid completion:(void (^)(BOOL))completion{
    if (tid.length == 0) {
        if (completion) {//暂时没看到接口
            completion(NO);
        }
    }
    if ([UserAccesser userId].length > 0) {
        if (completion) {//暂时没看到接口
            completion(NO);
        }
    }else{
        if (completion) {
            completion([collectArray containsObject:tid]);
        }
    }
}

@end
